package org.pageley.games.sanctuary.domain.environment;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;

import org.pageley.games.sanctuary.domain.Location;
import org.pageley.games.sanctuary.domain.entity.Entity;
import org.pageley.games.sanctuary.domain.environment.room.Room;

public class CompositeEnvironment extends AbstractEnvironment {

	private Collection<Room> rooms;
	private Collection<Location> nodes;
	
	public CompositeEnvironment(Collection<Room> rooms) {
		this.rooms = new ArrayList<Room>(rooms);
	}

	public Collection<Room> getRooms() {
		return Collections.unmodifiableCollection(this.rooms);
	}

	@Override
	public Collection<Location> getNodes() {
		if(nodes == null) {
			nodes = new HashSet<Location>();
			for(Room room : rooms) {
				nodes.addAll(room.getNodes());
			}
		}
		Collection<Location> result = new HashSet<Location>(nodes);
		for(Entity entity : getEntities()) {
			if(result.contains(entity.getLocation())) {
				result.remove(entity.getLocation());
			}
		}
		return result;
	}

}
